package com.mvd.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.adapter.BuHeGe3Adapter;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.BuHeGeModel3;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.mvd.app.commons.UtilsLog.e;

/***
 * 外检不合格项目第三级
 * **/
public class WaiJianThirdResultActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.title_btn_left)
    ImageButton titleBtnLeft;
    @BindView(R.id.title_btn_left_txt)
    Button titleBtnLeftTxt;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.iv_headtip)
    ImageView ivHeadtip;
    @BindView(R.id.title_btn_right_txt)
    Button titleBtnRightTxt;
    @BindView(R.id.title_btn_right)
    ImageButton titleBtnRight;
    @BindView(R.id.lv_main)
    ListView lvMain;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;

    private WaiJianThirdResultActivity instances = null;
    private BaseApplication baseApplication;
    private List<BuHeGeModel3> buHeGeModel3List = null;
    private BuHeGe3Adapter buHeGe3Adapter;
    private CodeModel codeModel = new CodeModel();
    private Intent intent;
    private String Code1, Code2, Flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wai_jian_third_result);
        ButterKnife.bind(this);
        setBackBtn();
        setTopTitle("不合格项目列表");
        baseApplication = BaseApplication.getSelf();
        instances = this;
        lvMain.setOnItemClickListener(this);
        intent = getIntent();
        if (getIntent() != null) {
            Code1 = intent.getExtras().getString("w_code1", "");
            Code2 = intent.getExtras().getString("w_code2", "");
            Flag = intent.getExtras().getString("w_Flag", "");

            if (!TextUtils.isEmpty(Code1) && !TextUtils.isEmpty(Code2)) {
                codeModel.setCode1(Code1);
                codeModel.setCode2(Code2);
            } else {
                UtilsLog.e("tag", "WaiJianThirdResultActivity===没有传递过来的数据");
            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (codeModel != null) {
            getData();
        }
    }

    /**
     * 获取不合规格项目第三级
     **/
    private void getBuHeGe3() {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(codeModel);
        UtilsLog.e("getBuHeGe3---jsonStr==" + jsonStr);
        OkGo.<String>post(SharedPreferencesUtils.getIP(instances) + BUHEGE_THRID)
                .tag(instances)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body().trim();
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
//                        UtilsLog.e("getBuHeGe3-result==" + result);
                        UtilsLog.e("getBuHeGe3-result=22=" + newResult);
                        CommonUtils.hideLoadingDialog(instances);
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(result)) {
                            buHeGeModel3List = new ArrayList<BuHeGeModel3>();
                            buHeGeModel3List = JsonUtil.stringToArray(newResult,
                                    BuHeGeModel3[].class);
                            if (buHeGeModel3List != null && buHeGeModel3List.size() > 0) {
                                buHeGe3Adapter = new BuHeGe3Adapter(instances, buHeGeModel3List, null, Flag);
                                lvMain.setAdapter(buHeGe3Adapter);
                            }
                        }
                        String logStr = "\n" + "不合格项目--请求成功" +
                                "\n" + "URL::" + SharedPreferencesUtils.getIP(instances) +
                                UPDATE_CAR_STATUS + "\n" + "\n" + "result::" + newResult + "\n";
                        PDALogUtils.createLogFile(5, logStr.getBytes());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        UtilsLog.e("getBuHeGe3-onError==" + response.body());
                        CommonUtils.hideLoadingDialog(instances);
//                        String logStr = "\n" + "路试--更新状态--失败--error--" +
//                                "\n" + "URL::" + SharedPreferencesUtils.getIP(instances) +
//                                UPDATE_CAR_STATUS + "\n" + "result::" + response.body();
//                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                    }
                });
    }

    private void getData() {
        if (CommonUtils.isOnline(instances)) {
            getBuHeGe3();
        } else {
            Toast.makeText(instances, getResources().getString(R.string.network_error), Toast
                    .LENGTH_LONG).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        instances = null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        // 取得ViewHolder对象
        BuHeGe3Adapter.ViewHolder viewHolder = (BuHeGe3Adapter.ViewHolder) view.getTag();
        if (viewHolder.mCheckBox.isChecked()) {
            viewHolder.mCheckBox.setChecked(false);
//            viewHolder.mCheckBox.setBackground(instances.getResources().getDrawable(R.drawable.i_no));
            UtilsLog.e("mCheckBox", position + "==取消了选择");
            buHeGeModel3List.get(position).setIsCheck("0");
        } else {
            viewHolder.mCheckBox.setChecked(true);

//            viewHolder.mCheckBox.setBackground(instances.getResources().getDrawable(R.drawable.i_hege));
            buHeGeModel3List.get(position).setIsCheck("1");
            UtilsLog.e("mCheckBox", buHeGeModel3List.get(position).toString());
        }
        boolean check = viewHolder.mCheckBox.isChecked();
        BuHeGe3Adapter.map.put(position, check);
        if (check) {
            BaseApplication.BaseApplication_buHeGeModel3List.add(buHeGeModel3List.get(position));
        }
        if (BuHeGe3Adapter.map.containsValue(false)) {
            //有选择的操作，即不是全部选中状态
            BaseApplication.IsAllSelect = false;
        }
//        if (!BuHeGe3Adapter.map.containsValue(false)) {
//            BaseApplication.IsAllSelect = true;
//        }

        if (BaseApplication.BaseApplication_buHeGeModel3List.size() > 0
                && BaseApplication.BaseApplication_buHeGeModel3List != null
                ) {
            for (int i = 0; i < BaseApplication.BaseApplication_buHeGeModel3List.size(); i++) {
                UtilsLog.e("不合格项目--第三级选择结果===" + BaseApplication.BaseApplication_buHeGeModel3List.get(i).toString());
            }
        }

    }

    class CodeModel {
        String Code1, Code2;

        public String getCode1() {
            return Code1;
        }

        public void setCode1(String code1) {
            Code1 = code1;
        }

        public String getCode2() {
            return Code2;
        }

        public void setCode2(String code2) {
            Code2 = code2;
        }
    }
}
